package android_dhbw.gpx;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;


public class OptionsActivity extends Activity {
	
	private TextView etxt_massDriver;
	private TextView etxt_massBike;
	private TextView etxt_avgV;
	private TextView etxt_aeroRes;
	private TextView etxt_rollRes;
	private TextView etxt_wTotal;
	private TextView etxt_frontSurface;
	private Button btn_save;
	private Spinner spn_mapsource;
	
	private SharedPreferences prefs;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
	    setContentView(R.layout.settings);
	    prefs = this.getSharedPreferences("android_dhbw.gpx", Context.MODE_PRIVATE);
	    
	    etxt_massDriver = (TextView)findViewById(R.id.settings_etxt_drivermass);
	    etxt_massBike = (TextView)findViewById(R.id.settings_etxt_bikemass);
	    etxt_avgV = (TextView)findViewById(R.id.settings_etxt_avgv);
	    etxt_aeroRes = (TextView)findViewById(R.id.settings_etxt_resaero);
	    etxt_rollRes = (TextView)findViewById(R.id.settings_etxt_resroll);
	    etxt_wTotal = (TextView)findViewById(R.id.settings_etxt_wTotal);
	    etxt_frontSurface = (TextView)findViewById(R.id.settings_etxt_frontSurface);
	    spn_mapsource = (Spinner)findViewById(R.id.settings_spn_mapsource);
	    
	    btn_save = (Button)findViewById(R.id.settings_btn_save);
	    btn_save.setOnClickListener(btnSaveListener);
	    
	    etxt_massDriver.setText(String.valueOf(prefs.getFloat("DRIVERMASS", (float) 70.0)));
	    etxt_massBike.setText(String.valueOf(prefs.getFloat("BIKEMASS", (float) 20.0)));
	    etxt_avgV.setText(String.valueOf(prefs.getFloat("AVGVELOCITY", (float) 7.0)));
	    etxt_aeroRes.setText(String.valueOf(prefs.getFloat("AERORESISTANCE", (float) 0.9)));
	    etxt_rollRes.setText(String.valueOf(prefs.getFloat("ROLLRESISTANCE", (float) 0.0045)));
	    etxt_wTotal.setText(String.valueOf(prefs.getFloat("WTOTAL", (float) 609000)));
	    etxt_frontSurface.setText(String.valueOf(prefs.getFloat("FRONTSURFACE", (float) 0.5)));
	    spn_mapsource.setSelection(prefs.getInt("SETTINGS_SPINNER_POSITION", 0));
	   
	    // TODO Auto-generated method stub
	}
	
	private OnClickListener btnSaveListener = new OnClickListener(){
		public void onClick(View v){
			
			Log.i("SPINNER:ID", Long.toString(spn_mapsource.getSelectedItemId()));
			Log.i("SPINNER:POSITION", Integer.toString(spn_mapsource.getSelectedItemPosition()));
			prefs.edit().putFloat("DRIVERMASS", Float.valueOf(etxt_massDriver.getText().toString())).commit();
			prefs.edit().putFloat("BIKEMASS", Float.valueOf(etxt_massBike.getText().toString())).commit();
			prefs.edit().putFloat("AVGVELOCITY", Float.valueOf(etxt_avgV.getText().toString())).commit();
			prefs.edit().putFloat("AERORESISTANCE", Float.valueOf(etxt_aeroRes.getText().toString())).commit();
			prefs.edit().putFloat("ROLLRESISTANCE", Float.valueOf(etxt_rollRes.getText().toString())).commit();
			prefs.edit().putFloat("WTOTAL", Float.valueOf(etxt_wTotal.getText().toString())).commit();
			prefs.edit().putFloat("FRONTSURFACE", Float.valueOf(etxt_frontSurface.getText().toString())).commit();
			prefs.edit().putInt("SETTINGS_SPINNER_POSITION", spn_mapsource.getSelectedItemPosition()).commit();
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
			
		}
	};


}
